**************************************************
********* Table A8: Latina/o and Non-White *******
********* Incumbents by State ********************
**************************************************

ssc install unique

do "do_files/prep_GRACE_data/load_prep_GRACE_data.do"

mat table_A8 = J(49,4,0)
local j = 1
quietly levelsof v02
foreach state in `r(levels)' {
  di "processing state: `state'"
  
  quietly unique graceuniqid if basket_white!=1 & v22==1 & v02=="`state'"
  mat table_A8[`j',3] = r(unique)
  
  quietly unique graceuniqid if basket_white!=1 & v22==1 & basket_women==0 & v02=="`state'"
  if r(unique) / table_A8[`j',3] == .{
    mat table_A8[`j',4] = 0  
  } 
  else {
    mat table_A8[`j',4] = r(unique) / table_A8[`j',3]
  }
  
  quietly unique graceuniqid if basket_lat==1 & v22==1 & v02=="`state'"
  mat table_A8[`j',1] = r(unique)

  quietly unique graceuniqid if basket_latm==1 & v22==1 & v02=="`state'"
  if r(unique) / table_A8[`j',1] == . {
    mat table_A8[`j',2] = 0  
  } 
  else {
    mat table_A8[`j',2] = r(unique) / table_A8[`j',1]
  }
  
  
  local ++ j
}

quietly levelsof v02
mat rownames table_A8 = `r(levels)'
mat colnames table_A8 = "Latx incumbents" "Male prop latx incumbents" "Nonwht incumbents" "Male prop nonwht incumbents" 
esttab matrix(table_A8)

* pausing for users who might be looping through all replication files
pause

